package de.g18.gruppe3.common.model;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;


/**
 * Enthält zusätzliche Attribute die in der LDAP-Schnittstelle benötigt werden.
 *
 * @author <a href="mailto:kevinhuber.kh@gmail.com">Kevin Huber</a>
 */
public class AbstractLDAPModel extends AbstractModel {

    public static final String PROPERTYNAME_DISTINGUISHED_NAME = "distinguishedName";
    public static final String PROPERTYNAME_PERSISTED = "persisted";

    private String distinguishedName;
    private boolean persisted;


    protected AbstractLDAPModel() {
        addPropertyChangeListener(new PersistenceChangeHandler());
        setPersisted(false);
    }

    public String getDistinguishedName() {
        return distinguishedName;
    }

    public void setDistinguishedName(String aNewValue) {
        String oldValue = getDistinguishedName();
        distinguishedName = aNewValue;
        firePropertyChange(PROPERTYNAME_DISTINGUISHED_NAME, oldValue, getDistinguishedName());
    }

    public boolean isPersisted() {
        return persisted;
    }

    public void setPersisted(boolean aNewValue) {
        boolean oldValue = isPersisted();
        persisted = aNewValue;
        firePropertyChange(PROPERTYNAME_PERSISTED, oldValue, isPersisted());
    }


    // Inner Classes ***********************************************************

    /**
     * Reagiert auf alle Änderungen am Model und setzt den Persisted Status des Models zurück.
     */
    private class PersistenceChangeHandler implements PropertyChangeListener {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals(PROPERTYNAME_PERSISTED)) {
                return;
            }
            setPersisted(false);
        }
    }
}
